import sys
from PyQt5.QtWidgets import QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem,QHeaderView,QAbstractItemView,QVBoxLayout
from PyQt5.QtGui import QStandardItem
from .word_memory import get_remember_record,get_v_name_by_vid

class RecordTableWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.record_list  = []
        # self.initUI()
    def show(self) -> None:
        super(RecordTableWidget, self).show()
        get_remember_record()
        self.initUI()
    def initUI(self):

        self.setWindowTitle("单词记忆记录")
        self.resize(550,450)
        conLayout = QHBoxLayout()
        self.tableWidget = QTableWidget()

        self.record_list = get_remember_record()
        print(len(self.record_list))
        self.tableWidget.setRowCount(len(self.record_list))
        self.tableWidget.setColumnCount(4)
        conLayout.addWidget(self.tableWidget)
        self.tableWidget.setHorizontalHeaderLabels(['单词', '记忆时间', '下次回顾时间','记忆质量'])

        for row in range(len(self.record_list)):
            record_tuple = self.record_list[row]
            for column in range(4):
                if column == 0:
                    name = get_v_name_by_vid(record_tuple[column+2])
                    if name is None:
                        break
                    else:
                        item = QTableWidgetItem(name)
                elif column == 3:
                    item = QTableWidgetItem(str(record_tuple[column + 2]))
                else:
                    item = QTableWidgetItem(record_tuple[column+2])
                self.tableWidget.setItem(row,column,item)
        self.setLayout(conLayout)

    def closeEvent(self, event):
        """
            重写closeEvent方法，实现dialog窗体关闭时执行一些代码
            :param event: close()触发的事件
            :return: None
        """
        self.record_list = get_remember_record()
        event.accept()




if __name__ == '__main__':
    pass
    # app = QApplication(sys.argv)
    # table = MyTableWidget()
    # table.show()
    # sys.exit(app.exec_())
